#include "setting_service_dao.h"
#include "sqliteutils.h"
#include "model/setting.h"
#include <QDebug>

SettingServiceDao::SettingServiceDao() {}
//获取设置
Setting* SettingServiceDao::getSetting(const QString &key) {
    QList<Setting*> settings = DatabaseManager::instance().select<Setting>(
        QString("key='%1'").arg(key));
    qDebug() << "查询到的设置数量:" << settings.size() ;
    if (!settings.isEmpty()) {
         Setting* result = settings.first();
         //qDebug() << "返回的指针地址:" << result;
         // 检查对象是否有效
         // if (result) {
         //     qDebug() << "对象类型:" << typeid(*result).name();
         //     qDebug() << "对象属性列表:";
         //     for (int i = 0; i < result->metaObject()->propertyCount(); ++i) {
         //         qDebug() << "属性:" << result->metaObject()->property(i).name();
         //     }
         // }
         return result;
    }
    return nullptr;
}
//查询全部设置转为map 格式
QVariantMap SettingServiceDao::getAllSettings(const QString &where) {
    QList<Setting*> settings = DatabaseManager::instance().select<Setting>(where);
    QVariantMap settingsMap;
    if (!settings.isEmpty()) {
        //将 settings 转为 QVariantMap
        for (Setting* setting : settings) {
            settingsMap.insert(setting->key, setting->value);
        }
    }
    return settingsMap;
}
//插入
bool SettingServiceDao::insertSetting(Setting *setting) {
    return DatabaseManager::instance().insert(*setting);
}
//更新
bool SettingServiceDao::updateSetting(Setting *setting) {
    return DatabaseManager::instance().update(*setting,
        QString("key='%1'").arg(setting->key));
}
//删除
bool SettingServiceDao::deleteSetting(const QString &key) {
    Setting tempSetting;
    return DatabaseManager::instance().remove(tempSetting,
        QString("key='%1'").arg(key));
}

